// +----------------------------------------------------------------------
// | CmsWing [ 美业管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 psi All rights reserved.
// +----------------------------------------------------------------------
// | Author: huang <2374266244@qq.com>
// +----------------------------------------------------------------------
'use strict';
import Base from './base.js';

export default class extends Base {
	// 获取产品品牌列表
	async brandlistAction(){
		let page = this.post();
		let pages = page.pages;
		let limits = page.limits;
		let search = page.search;
		let zt = page.zt;
		let shopid = this.userinfo.departmentid;

		let data = await this.model('or_brand')
		.alias('b')
		.field('b.*,f.name as fname')
		.join('`think_or_factory` f on f.id=b.belongs')
		.page(pages, limits)
		.order('b.id DESC')
		.where({'b.departmentid':shopid, 'b.isdisabled':zt, 'b.name|b.remarks|f.name':['like','%'+search+'%']})
		.countSelect();

		return this.json(data);
	}

	// 获取项目品牌列表
	async getsubjectbrandsAction(){
		let clist = await this.model('or_brand').where({departmentid:this.userinfo.departmentid,belongs:0}).select();
		let data = {
				"draw": '',
				"data": clist,
				"breadcrumb":''
			}
		return this.json(data);
	}

	// 获取品牌对应的厂家列表
	async getfactoryAction(){
		if (this.isPost) {
			let factoryid = JSON.parse(this.post('id'));
			let data = [];
			if (!think.isEmpty(factoryid)) {
				data = await this.model('or_factory').where({id:['IN',factoryid]}).select();
			}
			return data?this.json(data):this.fail({info:'无厂家'});
		}else{
			let data = await this.model('or_factory').where({departmentid:this.userinfo.departmentid}).select();
			return this.json(data);
		}

	}

	// 停用、启用厂家品牌
	async brandstatusAction(){
		let data = this.post();

		let shiyong = await this.isuse(data.id);
		if (!think.isEmpty(shiyong)) return this.fail(shiyong.ms);
		
		let res = await this.model('or_brand').where({id:data.id}).update({isdisabled:data.st});
		if (data.st*1) return res?this.success('停用成功'):this.fail('停用失败');
		else return res?this.success('启用成功'):this.fail('启用失败');
	}

	// 添加、编辑品牌
	async addeditAction(){
		let data = this.post();
		// 编辑品牌
		if (data.id*1) {
			let shiyong = await this.isuse(data.id);
			if (!think.isEmpty(shiyong)) return this.fail(shiyong.ms);
			let upd = await this.model('or_brand').where({id: data.id}).update({name:data.name,remarks: data.remarks, belongs: data.belongs});
			return upd?this.success('编辑成功'):this.fail('编辑失败');
		}
		// 新增品牌
		let add = {
			name: data.name,
			remarks: data.remarks,
			departmentid: this.userinfo.departmentid,
			belongs: data.belongs,
			addtime: Math.round(new Date().valueOf()/1000),
			isdisabled:data.radio,
		}
		let res = await this.model('or_brand').add(add);
		return res?this.success('添加成功'):this.fail('添加失败');
	}

	// 判断当前操作品牌是否被使用
	async isuse(brandid){
		let orid = this.userinfo.departmentid;
		let ongoods = await this.model('or_goods').where({departmentid: orid, brandid: brandid}).select();
		if (!think.isEmpty(ongoods)) return {tp:1,ms:'当前品牌已被产品使用'};
		let onsubject = await this.model('or_subject').where({departmentid: orid, brandid: brandid}).select();
		if (!think.isEmpty(onsubject)) return {tp:2,ms:'当前品牌已被项目使用'};
	}
}